package com.hm.pan.service.impl;

import java.sql.Timestamp;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.hm.pan.mapper.UserFileMapper;
import com.hm.pan.model.UserFile;
import com.hm.pan.service.UserFileService;
import com.hm.pan.util.StringUtil;

@Service
public class UserFileServiceImpl implements UserFileService {

	@Resource
	private UserFileMapper userFileMapper;

	@Override
	public List<UserFile> getAllFiles(UserFile filter) throws Exception {
		return userFileMapper.getAllFiles(filter);
	}

	@Override
	public UserFile getOneFile(UserFile filter) throws Exception {
		return userFileMapper.getOneFile(filter);
	}

	@Override
	public UserFile getOneFileByPath(String path) throws Exception {
		if (!StringUtil.isEmpty(path) && path.startsWith("/")) {
			// /编程/Java/Main.java
			String[] split = path.split("/+");

			if (split.length == 0) {
				UserFile userFile = new UserFile();
				userFile.setUserfileId(0L);
				userFile.setFileType(false);
				return userFile;
			}

			long userfileId = 0;
			// 以/进行分割，前面会有一个空元素，所以要从1开始
			int i = 1;

			// 按路径列举某文件夹下的所有文件所有文件
			for (; i < split.length; i++) {

				// 循环切割路径字符串
				UserFile f1 = new UserFile();
				f1.setFatherId(userfileId);
				f1.setFileName(split[i]);

				// 根据名字和父文件夹找文件夹
				UserFile file = this.getOneFile(f1);

				System.out.println(JSON.toJSON(file));

				// 没找到，说明路径错误
				if (file == null) {
					break;
				}

				// 不是最后一个元素，并且也不是目录，路径错误
				if (file.getFileType() && i != split.length - 1) {
					break;
				}

				if (i == split.length - 1) {
					return file;
				}

				userfileId = file.getUserfileId();
			}
		}

		return null;
	}

	@Override
	public List<UserFile> getAllFilesByPath(String path, UserFile filter) throws Exception {
		if (!StringUtil.isEmpty(path) && path.startsWith("/")) {
			String[] split = path.split("/+");

			Long fatherId = 0L;
			// 以/进行分割，前面会有一个空元素，所以要从1开始
			int i = 1;

			// 按路径列举某文件夹下的所有文件所有文件
			for (; i < split.length; i++) {

				// 循环切割路径字符串
				UserFile f1 = new UserFile();
				f1.setFatherId(fatherId);
				f1.setFileName(split[i]);
				f1.setFileType(false);

				// 根据名字和父文件夹找文件夹
				UserFile file = this.getOneFile(f1);

				// 没找到，说明路径错误
				if (file == null) {
					break;
				}

				fatherId = file.getUserfileId();
			}

			// 循环没有被打断，说明正确找到了
			if (i == split.length || split.length == 0) { // 后面这个等于0是因为如果直接是/，分割后就会没有任何元素
				filter.setFatherId(fatherId);
				return this.getAllFiles(filter);
			}
		}

		return null;
	}

	@Override
	public List<UserFile> getRecycle(UserFile filter) {
		return userFileMapper.getRecycle(filter);
	}

	@Override
	public boolean deleteFile(boolean absolutely, long userfileId) throws Exception {
		UserFile filter = new UserFile();
		filter.setUserfileId(userfileId);
		UserFile oneFile = this.getOneFile(filter);

		if (oneFile == null) {
			return false;
		}

		if (!oneFile.getFileType()) {
			UserFile filter2 = new UserFile();
			filter2.setFatherId(oneFile.getUserfileId());
			List<UserFile> children = this.getAllFiles(filter2);
			for (UserFile child : children) {
				deleteFile(absolutely, child.getUserfileId());
			}
		}

		if (!absolutely) {
			UserFile userFile = new UserFile();
			userFile.setUserfileId(userfileId);
			userFile.setIsDelete(true);
			return this.updateUserFile(userFile);
		}

		return userFileMapper.deleteFile(userfileId);
	}

	@Override
	public boolean updateUserFile(UserFile userFile) throws Exception {
		return userFileMapper.updateUserFile(userFile);
	}

	@Override
	public boolean restoreFile(long userfileId) throws Exception {
		UserFile filter = new UserFile();
		filter.setUserfileId(userfileId);
		filter.setIsDelete(true);
		UserFile oneFile = this.getOneFile(filter);

		if (oneFile == null) {
			return false;
		}

		if (!oneFile.getFileType()) { // 文件夹
			UserFile filter2 = new UserFile();
			filter2.setFatherId(oneFile.getUserfileId());
			List<UserFile> children = this.getAllFiles(filter2);
			for (UserFile child : children) {
				restoreFile(child.getUserfileId());
			}
		}

		UserFile userFile = new UserFile();
		userFile.setUserfileId(userfileId);
		userFile.setIsDelete(false);

		return this.updateUserFile(userFile);
	}

	@Override
	public boolean insertFile(UserFile userFile) throws Exception {
		UserFile userFile2 = new UserFile();
		userFile2.setFatherId(userFile.getFatherId());
		userFile2.setFileName(userFile.getFileName());
		UserFile oneFile = getOneFile(userFile2);
		
		if(oneFile != null) {
			return false;
		}
		
		userFile.setUploadTime(new Timestamp(System.currentTimeMillis()));
		userFile.setIsDelete(false);
		
		return userFileMapper.insertFile(userFile);
	}

	@Override
	public List<UserFile> deepGetAllFiles(long userfileId) throws Exception {
		return userFileMapper.deepGetAllFiles(userfileId);
	}

}
